/*
 * @Description: 
 * @Author: Haor
 * @Date: 2022-08-27 13:22:08
 * @LastEditTime: 2022-08-28 17:33:37
 * @LastEditors: Haor
 */
import { Controller, Get, Post, Body, UseInterceptors, UploadedFile, Param, Res } from "@nestjs/common";
import { FileInterceptor, FileFieldsInterceptor } from '@nestjs/platform-express'
import { Express, Response } from "express";
import { FileService } from './file.service'
import { PagerDto } from "@dto/public.dto";
import { NoAuth, Permission } from "@decorators";
import * as path from 'path'
import { FileEntity } from "@entities/file.entity";
import { CatchError } from "@decorators/cache-error.decorator";
import { FileFilingDTO } from "@dto/file.dto";

@Controller('file')
export class FileController {
	constructor(private readonly fileService: FileService) {}

    @Post()
    @Permission('file::list')
    getFileList(@Body() params: PagerDto) {
        return this.fileService.getFileList(params)
    }

    @Post('upload')
    @Permission('file::upload')
    @UseInterceptors(FileInterceptor('file'))
    updateFile(@UploadedFile() file: Express.Multer.File): any {
	    return this.fileService.updateFile(file)
    }

    @Get('download/:id')
    @NoAuth()
    @Permission('file::download')
    @CatchError('文件下载')
    async download(@Param('id') fileId: number, @Res() res: Response) {
	    const file: FileEntity = await this.fileService.download(+fileId)
        res.download(path.join(process.cwd(), file.path), file.originName, (err) => {
            if (!err) {
                return console.log('success', 'view', file.path);
            }
        });
    }

    @Get('/list/:bizId')
    getFileListByBizId(@Param('bizId') bizId: string) {
        return this.fileService.getFileListByBizId(+bizId)
    }

    /**
     * 文件落盘
     */
    @Post('/filing')
    fileFiling(@Body() data: FileFilingDTO) {
        return this.fileService.filing(data)
    }

    @Post('/bind_and_filing')
    bindBizAndFiling(@Body() data: FileFilingDTO) {
        return this.fileService.bindBizAndFiling(data)
    }
}
